package com.pachong.android.framework.picture.browser.view;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.bumptech.glide.load.resource.bitmap.GlideBitmapDrawable;
import com.pachong.android.baseuicomponent.fragment.BaseFragment;
import com.pachong.android.framework.R;
import com.pachong.android.frameworkbase.imageloader.ImageLoader;
import com.pachong.android.frameworkbase.utils.BitmapUtil;

import uk.co.senab.photoview.PhotoView;

/**
 * Created by Zoi.
 * E-mail：KyluZoi@gmail.com
 * 2016/6/8
 */
public class PictureFragment extends BaseFragment {

    private PhotoView mPvPhoto;
    private String mSavePath = null;
    private SaveImpl imp;

    public void setImp(SaveImpl imp) {
        this.imp = imp;
    }

    public static PictureFragment newInstance(String photoData) {
        return newInstance(photoData, "");
    }

    public static PictureFragment newInstance(String photoData, String filePath) {
        Bundle args = new Bundle();
        PictureFragment fragment = new PictureFragment();
        args.putString("photo", photoData);
        args.putString("filepath", filePath);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frag_photo, null);
        mPvPhoto = (PhotoView) view.findViewById(R.id.pv_photo);
        if (getArguments() != null) {
            String url = getArguments().getString("photo");
            mSavePath = getArguments().getString("filepath");
            ImageLoader.with(getContext())
                    .load(url)
                    .into(mPvPhoto)
                    .start();
        }
        return view;
    }


    public void saveBitmap() {
        Bitmap bitmap = ((GlideBitmapDrawable) (mPvPhoto.getDrawable())).getBitmap();
        Log.d("PictureFragment", (mSavePath + "/aaaaa.png"));
        //save as absPath like up
        boolean succ = BitmapUtil.saveBitmap(bitmap, mSavePath+ "/aaaaa.png" , true);
        if (succ) {
            Toast.makeText(getContext(), "保存成功", Toast.LENGTH_SHORT).show();
            imp.saveFileSuccess(mSavePath);
        } else {
            Toast.makeText(getContext(), "保存失败", Toast.LENGTH_SHORT).show();
        }

    }



    public interface SaveImpl {
        void saveFileSuccess(String filePath);
    }

}
